package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.bridge.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private com.sky.mapper.setmealDishMapper setmealDishMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSetmeal(SetmealDTO dto) {
        Setmeal setmeal = Setmeal.builder()
                .build();
        BeanUtils.copyProperties(dto, setmeal);

        setmealMapper.addSetmeal(setmeal);


        List<SetmealDish> dishes = dto.getSetmealDishes();
        if (dishes != null && !dishes.isEmpty()) {
            dishes.forEach(dish -> {
               dish.setSetmealId(setmeal.getId());
            });
        }

        setmealDishMapper.addSetmealDishs(dishes);

    }

    @Override
    public PageResult page(SetmealPageQueryDTO queryDTO) {

        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());

       Page<SetmealVO> p = (Page<SetmealVO>) setmealMapper.page(queryDTO.getName(),queryDTO.getStatus(),queryDTO.getCategoryId());

        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.updateSetmealStatus(setmeal);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealById = setmealMapper.getSetmealById(id);
        List<SetmealDish> setmealDishById = setmealDishMapper.getSetmealDishById(id);
        setmealById.setSetmealDishes(setmealDishById);
        return setmealById;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSetmealById(List<Long> ids) {
        List<Integer> id = setmealMapper.getStatusById(ids);
        id.forEach(status -> {
            if (status == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });


        setmealMapper.deleteSetmealById(ids);
        setmealDishMapper.delteSetmealDishByIds(ids);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSetmeal(SetmealDTO dto) {
        Setmeal setmeal = Setmeal.builder().build();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.updateSetmeal(setmeal);

        setmealDishMapper.delteSetmealDishById(setmeal.getId());

        List<SetmealDish> dishes = dto.getSetmealDishes();
        dishes.forEach(dish -> {
            dish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.addSetmealDishs(dishes);


    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
